from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

db = SQLAlchemy()

class BaziRecord(db.Model):
    """八字记录表"""
    __tablename__ = 'bazi_record'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='记录ID')
    bazi_id = db.Column(db.String(32), unique=True, nullable=False, comment='八字ID')
    user_id = db.Column(db.BigInteger, db.ForeignKey('user.id'), nullable=True, comment='用户ID（游客为空）')
    name = db.Column(db.String(50), nullable=True, comment='姓名')
    gender = db.Column(db.SmallInteger, nullable=False, comment='性别：1-男，2-女')
    birth_year = db.Column(db.Integer, nullable=False, comment='出生年')
    birth_month = db.Column(db.Integer, nullable=False, comment='出生月')
    birth_day = db.Column(db.Integer, nullable=False, comment='出生日')
    birth_hour = db.Column(db.Integer, nullable=False, comment='出生时')
    lunar_year = db.Column(db.Integer, nullable=False, comment='农历年')
    lunar_month = db.Column(db.Integer, nullable=False, comment='农历月')
    lunar_day = db.Column(db.Integer, nullable=False, comment='农历日')
    lunar_hour = db.Column(db.Integer, nullable=False, comment='农历时')
    tian_gan = db.Column(db.String(20), nullable=False, comment='天干')
    di_zhi = db.Column(db.String(20), nullable=False, comment='地支')
    wu_xing = db.Column(db.String(20), nullable=False, comment='五行')
    na_yin = db.Column(db.String(20), nullable=False, comment='纳音')
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment='更新时间')

    user = db.relationship('User', backref=db.backref('bazi_records', lazy='dynamic'))

    def to_dict(self):
        return {
            'id': self.id,
            'bazi_id': self.bazi_id,
            'user_id': self.user_id,
            'name': self.name,
            'gender': self.gender,
            'birth_year': self.birth_year,
            'birth_month': self.birth_month,
            'birth_day': self.birth_day,
            'birth_hour': self.birth_hour,
            'lunar_year': self.lunar_year,
            'lunar_month': self.lunar_month,
            'lunar_day': self.lunar_day,
            'lunar_hour': self.lunar_hour,
            'tian_gan': self.tian_gan,
            'di_zhi': self.di_zhi,
            'wu_xing': self.wu_xing,
            'na_yin': self.na_yin,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        }


class BaziAnalysis(db.Model):
    """八字分析表"""
    __tablename__ = 'bazi_analysis'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='分析ID')
    bazi_id = db.Column(db.String(32), db.ForeignKey('bazi_record.bazi_id'), nullable=False, comment='八字ID')
    basic_summary = db.Column(db.Text, nullable=True, comment='基础分析摘要')
    character_summary = db.Column(db.Text, nullable=True, comment='性格特点摘要')
    destiny_analysis = db.Column(db.Text, nullable=True, comment='命运轨迹分析')
    career_analysis = db.Column(db.Text, nullable=True, comment='事业发展分析')
    wealth_analysis = db.Column(db.Text, nullable=True, comment='财运分析')
    relationship_analysis = db.Column(db.Text, nullable=True, comment='人际关系分析')
    health_analysis = db.Column(db.Text, nullable=True, comment='健康状况分析')
    general_suggestion = db.Column(db.Text, nullable=True, comment='一般建议')
    specific_suggestion = db.Column(db.Text, nullable=True, comment='针对性建议')
    is_paid = db.Column(db.SmallInteger, nullable=False, default=0, comment='是否已付费查看')
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment='更新时间')

    bazi_record = db.relationship('BaziRecord', backref=db.backref('analysis', uselist=False))

    def to_dict(self, include_paid_content=False):
        result = {
            'id': self.id,
            'bazi_id': self.bazi_id,
            'basic_summary': self.basic_summary,
            'character_summary': self.character_summary,
            'is_paid': self.is_paid,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        }
        
        if include_paid_content or self.is_paid:
            result.update({
                'destiny_analysis': self.destiny_analysis,
                'career_analysis': self.career_analysis,
                'wealth_analysis': self.wealth_analysis,
                'relationship_analysis': self.relationship_analysis,
                'health_analysis': self.health_analysis,
                'general_suggestion': self.general_suggestion,
                'specific_suggestion': self.specific_suggestion
            })
            
        return result
